package dojo.bowling;

public class Frame {

	private int firstThrow;
	private int secondThrow;
	private Frame nextFrame;

	public Frame getNextFrame() {
		return this.nextFrame;
	}

	public void setNextFrame(Frame nextFrame) {
		this.nextFrame = nextFrame;
	}

	public Frame(int firstThrow, int secondThrow) {
		this.setFirstThrow(firstThrow);
		this.setSecondThrow(secondThrow);
	}

	public void setFirstThrow(int firstThrow) {
		this.firstThrow = firstThrow;
	}

	public int getFirstThrow() {
		return this.firstThrow;
	}

	public void setSecondThrow(int secondThrow) {
		this.secondThrow = secondThrow;
	}

	public int getSecondThrow() {
		return this.secondThrow;
	}

	public boolean isSpare() {
		return this.secondThrow + this.firstThrow == 10
				&& this.firstThrow != 10;
	}

	public boolean isStrike() {
		return this.firstThrow == 10;
	}

	public int getScore() {

		return this.firstThrow + this.secondThrow + spareBonus()
				+ strikeBonus();
	}

	protected int spareBonus() {
		if (isSpare())
			return this.nextFrame.firstThrow;
		return 0;
	}

	protected int strikeBonus() {
		if (isStrike()) {
			int result = this.nextFrame.firstThrow;
			result += this.nextFrame.secondBall();
			return result;
		}
		return 0;
	}

	protected int secondBall() {
		if (this.isStrike()) {
			return this.nextFrame.firstThrow;
		}
		return this.secondThrow;
	}

}